<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/2 0002
 * Time: 14:09
 */

namespace app\admin\controller;


use think\Controller;

class Room extends Controller
{
    public function index(){
        $arr = db("room")->select();
        $data = $this->getList($arr);
        $this->assign([
            'data' => $data
        ]);
        return view("index");
    }
    public function add(){
        if(request()->isAjax()){
            $param = request()->except("image");
            $file = request()->file('image');
            $info=$file->move( './static/images');
            $filename=str_replace("\\","/",$info->getSaveName());
            $image = '/static/images/'.$filename;
            $data = [
                'pid' => $param['pid'],
                'title' => $param['title'],
                'image' => $image,
                'name' => $param['name'],
                'descr' => $param['descr'],
                'content' => $param['content'],
                'type' => 0,
//                '' =>,
                'source' =>$param['source'],
                'addtime' =>date("Y-m-d H:i:s",time())
            ];
            $add = db("room")->insert($data);
            if($add){
                return 200;
            }else{
                return -1;
            }
        }
        $arr = db("room")->select();
        $data = $this->getList($arr);
        $this->assign([
            'data' => $data
        ]);
        return view("add");
    }
    public function getList($brr, $pid = 0, &$arr = [], $level = 1)
    {
        foreach ($brr as $v) {
            if ($v['pid'] == $pid) {
                $v['level'] = $level;
                $arr[]      = $v;
                getList($brr, $v['id'], $arr, $level + 1);
            }
        }
        return $arr;
    }
    public function delete(){
        $id = input("id");
        $del = db("room")->delete($id);
        if($del){
            return 200;
        }else{
            return -1;
        }
    }
    public function detail(){
        $descr = input("descr");
        $content = input("content");
        if(!empty($content)){
            $content = db("room")->where("id",$content)->find();
            $this->assign("content",$content['content']);
            return view("detail");
        }
        $descr = db("room")->where("id",$descr)->find();
        $this->assign("descr",$descr['descr']);
        return view("detail");
    }
    public function edit(){
        if(request()->isAjax()){
            $id = input("id");
//            $data = db("room")->find($id);
            $param = request()->except("id");
            $file = request()->file('image');
            if(empty($file)){
                $data = [
                    'pid' => $param['pid'],
                    'name' => $param['name'],
                    'title' => $param['title'],
                    'descr' => $param['descr'],
                    'content' => $param['content'],
                    'image' => $param['old_image']
                ];
            }else{
                $info=$file->move( './static/images');
                $filename=str_replace("\\","/",$info->getSaveName());
                $image = '/static/images/'.$filename;
                $data = [
                    'pid' => $param['pid'],
                    'name' => $param['name'],
                    'title' => $param['title'],
                    'descr' => $param['descr'],
                    'content' => $param['content'],
                    'image' => $image
                ];
            }
            $res = db("room")->where("id",$id)->update($data);
            if($res){
                return 200;
            }else{
                return -1;
            }
        }
        $id = input("id");
        $data = db("room")->find($id);
        $arr = db("room")->select();
        $all_data = $this->getList($arr);
        $this->assign([
            'data' => $data,
            'all_data' => $all_data
        ]);
        return view("edit");
    }
}